import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class MyThreadPool {
    //创建一个阻塞队列用于组织任务
    BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(100);

    /**
     * 提交任务到线程池
     * @param runnable 提交的任务
     */
    public void submit(Runnable runnable) throws InterruptedException {
        if (runnable == null) {
            throw new IllegalArgumentException("任务不能为空！！");
        }
        queue.put(runnable);
    }
    public MyThreadPool(int ThreadNum) {
        if(ThreadNum <= 0) {
            throw new IllegalArgumentException("线程数不能小于等于0");
        }
        for (int i = 0; i < ThreadNum; i++) {
            Thread thread = new Thread(()-> {
                try {
                    //取出任务
                    Runnable runnable = queue.take();
                    //执行任务
                    runnable.run();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            thread.start();
        }
    }
}
